#!/usr/bin/env ruby
#
# This file is gererated by ruby-glade-create-template 1.1.4.
#
require 'libglade2'
require 'gtk2'
require 'yaml'

class ConfigmanagerGlade
  include GetText

  attr :glade
  
  def initialize(path_or_data, root = nil, domain = nil, localedir = nil, flag = GladeXML::FILE)
    bindtextdomain(domain, localedir, nil, "UTF-8")
    @glade = GladeXML.new(path_or_data, root, domain, localedir, flag) {|handler| method(handler)}
    
  end
  
  def on_save_button_clicked(widget)
    global = {
		'plugindir'			=>	@glade['plugindir'].text,
		'plugin_data_dir'	=>	@glade['plugin_data_dir'].text,
		'plugin_blacklist'	=>	@glade['plugin_blacklist'].text,
		'timestamp_format'	=>	@glade['timestamp_format'].text
	}
	network = {
		'server'			=>	@glade['server'].text,
		'port'				=>	@glade['port'].text,
		'username'			=>	@glade['username'].text,
		'nickname'			=>	@glade['nickname'].text,
		'realname'			=>	@glade['realname'].text,
		'commands'			=>	@glade['commands'].text.split("\\n")
		'highlight_words'	=>	@glade['highlight_words'].text.split(',')
	}
	config = {
		'global'	=>	global,
		'network'	=>	network
	}
	if !File.exists?("#{ENV['HOME']}/.xsirc") then
		Dir.mkdir("#{ENV['HOME']}/.xsirc")
	end
	File.open("#{ENV['HOME']}/.xsirc/config.yml",'w') do |file|
		file << config.to_yaml
	end
	dialog = Gtk::MessageDialog.new(@glade['window'],Gtk::Dialog::DESTROY_WITH_PARENT,Gtk::MessageDialog::INFO,Gtk::MessageDialog::BUTTONS_OK,"Restart XSirc for the changes to take effect.")
	dialog.run {|r| puts r}
	dialog.destroy
	Gtk.main_quit
  end
end

# Main program
if __FILE__ == $0
	# Set values as your own application. 
	PROG_PATH = "configmanager.glade"
	PROG_NAME = "XSIRC config manager"
	win=ConfigmanagerGlade.new(PROG_PATH, nil, PROG_NAME)
	win.glade['window'].show_all
	if File.exists?("#{ENV['HOME']}/.xsirc/config.yml") then
		config = YAML::load(File.open("#{ENV['HOME']}/.xsirc/config.yml"))
		for item in config['global']
			win.glade[item[0]].text = item[1]
		end
		for item in config['network']
			if item[1].class == Array then
				win.glade[item[0]].text = item[1].join('\n')
			else
				win.glade[item[0]].text = item[1]
			end
		end
	end
	Gtk.main
end
